<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>  
<html>  
    <head>  
    <meta charset="utf-8">  
    <title>数据源配置</title>
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/default/easyui.css" />
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/icon.css" />
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
   		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.1.2.2.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/locale/easyui-lang-zh_CN.js"></script>
	 	<script type="text/javascript">
			var pageNumber;
			var pageSize;
			var a = document.documentElement.clientHeight - 20;
			//站点
			var stations = new Array();
			//数据源类型
			var sourceTypes = new Array();
			function stationFormatter(value){
				for(var i=0; i<stations.length; i++){
					if (stations[i].staId == value) return stations[i].staName;
				}
				return value;
			}
			function sourceTypeFormatter(value){
				for(var i=0; i<sourceTypes.length; i++){
					if (sourceTypes[i].code == value) return sourceTypes[i].typeCname;
				}
				return value;
			}
			
			function loadSourceInfo(){
				$.ajax({
					url:'${pageContext.request.contextPath}/edi/loadSourceInfo',
					type:'post',
					data:{},
					success:function(data){
						//alert(data[0].length);//0  站点// 1数据源类型
						for(var i = 0; i < data[0].length; i++){
							var station = {};
							station.staId = data[0][i].staId;
							station.staName = data[0][i].staName;
							stations[i] = station;
							$('select[name=stationId]').append("<option value=\'"+ data[0][i].staId +"\'>"+data[0][i].staName+"</option>");
						}
						for(var i = 0; i < data[1].length; i++){
							var sourceType = {};
							sourceType.code = data[1][i].code;
							//alert(data[1][i].code);
							sourceType.typeCname = data[1][i].typeCname;
							sourceTypes[i] = sourceType;
							$('select[name=dataSourceType]').append("<option value=\'"+ data[1][i].code +"\'>"+data[1][i].typeCname+"</option>");
						}
						loadData();
					}
				});
			}
			
			$(function(){
				pageNumber = 1;
				pageSize = 15;
				a = document.documentElement.clientHeight -30;
				//添加窗口
				$('#kk').window({  
							    width:600,  
							    height:400,  
							    modal:true  
							});  
				$('#kk').window('close');  //关闭窗口 
				
				loadSourceInfo();
			});
			
			
			function loadData(){
				var lastIndex = -1;
				var addRow = -1;
				$('#tt').datagrid({  
				    url:'${pageContext.request.contextPath}/edi/queryExcGroup',  
				    width:function(){return document.documentElement.clientWidth - 30 },
					height:a,
					pageList:[20,30,50,100],
				    iconCls:'icon-save',
					nowrap: false,
					striped: true,				 
					collapsible:true,
					autoRowHeight: false,		
					sortName: 'code',
					sortOrder: 'desc',
					remoteSort: false,
					idField:'code',
					fitColumns:true,
					singleSelect:true,
				    columns:[[  
				        {field:'GROUP_ID',title:'GROUP_ID',width:100,hidden:true},  
				        {field:'GROUP_NAME',title:'分组名',width:100,editor:'text'},  
				        {field:'STATION_ID',title:'站点名',width:100,align:'right',
				        	 formatter:stationFormatter,
				        	editor:{type:'combobox',options:{valueField:'staId',textField:'staName',data:stations,required:true}}},  
				        {field:'DATA_SOURCE_TYPE',title:'数据源类型',width:100,align:'right',
				        	formatter:sourceTypeFormatter,
				        	editor:{type:'combobox',options:{valueField:'code',textField:'typeCname',data:sourceTypes,required:true}}},  
				        {field:'USER_NAME',title:'用户名',width:100,align:'right',editor:'text'},  
				        {field:'URL',title:'URL',width:100,align:'right',editor:'text'},  
				        {field:'MEMO',title:'备注',width:100,align:'right',editor:'text'}  
				    ]],
					pagination:true,
					rownumbers:true,
					toolbar:[{
						id:'btnadd',
						text:'添加',
						iconCls:'icon-add',
						handler:function(){
							$('#kk').window('open');  //打开窗口
							resetForm();
						}					
					},{
						id:'btncut',
						text:'删除',
						iconCls:'icon-cut',
						handler:function(){
							$('#btnsave').linkbutton('enable');
							alert('cut')
						}
					},'-',{
						id:'btnsave',
						text:'保存',
						disable:true,
						iconCls:'icon-save',
						handler:function(){
							$('#tt').datagrid('endEdit', lastIndex);
							//当有数据编辑时提示是否保存
					        var POSStockRowTable = $('#tt');
							//判断是否只有一行在修改
							if (lastIndex == -1) {
							     alert('提示    没有修改过的数据');
							}else{
							    //当有数据编辑时提示是否保存
							    $.messager.confirm('提示','是否保存所有修改过的数据', function(btn2){
							    //点击确定则保存,否则取消
								    if (btn2) {
								       alert("保存了修改过的数据"+rows.length);
								    }
								});
							}
						}
					}],
					onBeforeLoad:function(){
						$('#tt').datagrid('rejectChanges');
					},
					onDblClickRow:function(rowIndex){
						if (lastIndex != rowIndex){
							$('#tt').datagrid('endEdit', lastIndex);
							$('#tt').datagrid('beginEdit', rowIndex);
						}
						lastIndex = rowIndex;
					},
					onAfterEdit:function(rowIndex,rowData,changes){
						$.messager.confirm('提示', '是否保存修改后的数据', function(btn2){
				        //点击确定则保存,否则取消
					        if (btn2) {
								alert("保存数据");
					            //存放正在编辑的行号
					        }
					    });
					}
				});
			}
			
			function addExcGroup(){
				if(!checkForm()){
					$.messager.alert('提示',"请填入基本信息","warning");
					return;
				}
				$.ajax({
					url:'${pageContext.request.contextPath}/edi/addExcGroup',
					type:'post',
					data:$('#groupForm').serialize(),
					success:function(data){
						if(data.info == 'UpdateSuccess'){
							$.messager.alert('提示',"更新成功");
							$('#tt').datagrid('reload');
						}else if(data.info == 'SaveSuccess'){
							$.messager.alert('提示',"保存成功");
							$('input[name=groupId]').val(data.msg);
							$('#tt').datagrid('reload');
						}else{
							$.messager.alert('提示',data.info);
						}
					}
				});
			}
			
			function checkForm(){
				if($('input[name=groupName]').val()!='' && 
					$('select[name=dataSourceType]').val()!='00'){
					return true;
				}
				return false;
			}
			
			
			function resetForm(){
				$('input[name=groupName]').val('');
				$('input[name=userName]').val('');
				$('input[name=passwd]').val('');	
				$('input[name=groupId]').val('');
			}
	 	</script>
    </head>  
    <body>  
    	<!-- 数据源查询界面 -->
    	<table id="tt"></table>  
    	<div id="kk">
    		<!-- 数据源配置界面 -->
	    	<form id="groupForm">
		    	<table>
		    		<tr>
		    			<td>分组名:</td>
		    			<td colspan="3" style="width:465px;"><input name='groupName' style="width:100%" /><input name='groupId' style="display: none;"/></td>
		    		</tr>
		    		<tr>
		    			<td>站点:</td>
		    			<td style="width:175px;">
		    				<select style="width:100%" name='stationId'>
		    				</select>
		    			</td>
		    			<td>数据源类型:</td>
		    			<td>
		    				<select style="width:100%"  name='dataSourceType'>
		    					<option value='00'>==请选择==</option>
		    				</select>
		    			</td>
		    		</tr>
		    		<tr>
		    			<td>用户名:</td>
		    			<td colspan="3"><input name='userName' style="width:100%" /></td>
		    		</tr>
		    		<tr>
		    			<td>密码:</td>
		    			<td colspan="3"><input name='passwd' style="width:100%"  /></td>
		    		</tr>
		    		<tr>
		    			<td>URL:</td>
		    			<td colspan="3"><input name='url' style="width:100%"  /></td>
		    		</tr>
		    		<tr>
		    			<td>备注:</td>
		    			<td colspan="3"><textarea name='memo' cols="55" rows="3"></textarea></td>
		    		</tr>
		    		<tr>
		    			<td></td>
		    			<td colspan="3">
		    				<a href="#" class="easyui-linkbutton" onclick="javascript:addExcGroup()">保存</a> 
		    				<a href="#" class="easyui-linkbutton" onclick="javascript:resetForm()">添加</a> 
		    			</td>
		    		</tr>
		    	</table>
	    	</form>
    	</div>
	</body>  
</html>  